package com.ruoyi.project.online_experimentStu.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.online_experiment.domain.OnlineExperiment;
import com.ruoyi.project.online_experiment.service.IOnlineExperimentService;
import com.ruoyi.project.online_experimentStu.domain.LookExperiment;
import com.ruoyi.project.online_experimentStu.domain.OnlineExperimentStu;
import com.ruoyi.project.online_experimentStu.domain.OnlineExperimentTea;
import com.ruoyi.project.online_experimentStu.domain.Student;
import com.ruoyi.project.online_experimentStu.service.OnlineExperimentStuService;
import com.ruoyi.project.online_experimentStu.token.JwtUtil;
import com.ruoyi.project.system.dept.domain.Dept;
import com.ruoyi.project.system.user.domain.User;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @ClassName OnlineExperimentStuController
 * @Description TODO
 * @Author lvyongwei
 * @Date 2020-04-16 11:08
 **/
@Controller
@RequestMapping("/system/OnlineExperimentStu")
public class OnlineExperimentStuController extends BaseController {

    private String prefix = "system/onlineExperimentStu";

    @Autowired
    private IOnlineExperimentService iOnlineExperimentService;

    @Autowired
    private OnlineExperimentStuService onlineExperimentStuService;

    @RequiresPermissions("system:onlineExperimentStu:view")
    @GetMapping()
    public String experiment()
    {
        return prefix + "/onlineExperiment";
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @RequiresPermissions("system:onlineExperimentStu:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(OnlineExperiment onlineExperiment)
    {
        startPage();
        onlineExperiment.setAssignTeacher(getSysUser().getUserId());
        List<OnlineExperiment> list = iOnlineExperimentService.selectOnlineExperimentList(onlineExperiment);
        return getDataTable(list);
    }

    /**
     * 布置实验
     */
    @GetMapping("/edit/{onlineExperimentId}")
    public String edit(@PathVariable("onlineExperimentId") Long onlineExperimentId, ModelMap mmap)
    {
        OnlineExperiment onlineExperiment = iOnlineExperimentService.selectOnlineExperimentById(onlineExperimentId);
        //调用获取部门的方法
        List<Dept> depts=onlineExperimentStuService.getStudentDept();
        mmap.put("onlineExperiment", onlineExperiment);
        mmap.put("depts",depts);
        return prefix + "/edit";
    }

    /**
     * 下载模板
     * @return
     */
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<Student> util = new ExcelUtil<Student>(Student.class);
        return util.importTemplateExcel("导入学生模板");
    }

    /**
     * 模板上传
     * @param file
     * @param updateSupport
     * @return
     * @throws Exception
     */
    @Log(title = "用户管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport, OnlineExperimentTea onlineExperimentTea) throws Exception
    {
        /**
         * 设置创建实验的教师的id
         */
        onlineExperimentTea.setUserId(getSysUser().getUserId());
        String message="";
        if(file!=null){
            ExcelUtil<Student> util = new ExcelUtil<Student>(Student.class);
            List<Student> userList = util.importExcel(file.getInputStream());
            message = onlineExperimentStuService.experimentalSetup(onlineExperimentTea,userList,updateSupport);
        }else{
            message = onlineExperimentStuService.experimentalSetup(onlineExperimentTea,null,updateSupport);
        }
        return AjaxResult.success(message);
    }


    /**
     * 获取到所有已布置的实验页面
     * @return
     */
    @GetMapping("/getAllExperiment")
    public String experiment1(int onlineExperimentId,ModelMap map)
    {
        map.addAttribute("onlineExperimentId",onlineExperimentId);
        return prefix + "/onlineExperiment1";
    }


    /**
     * 获取到所有已布置的实验
     * @param onlineExperimentTea
     * @return
     */
    @RequiresPermissions("system:onlineExperimentStu:list")
    @PostMapping("/list1")
    @ResponseBody
    public TableDataInfo list1(OnlineExperimentTea onlineExperimentTea)
    {
        startPage();
        onlineExperimentTea.setUserId(getSysUser().getUserId());
        List<OnlineExperimentTea> lists=onlineExperimentStuService.getAllOnlineExperimentTea(onlineExperimentTea);
        return getDataTable(lists);
    }


    /**
     * 获取到已经做过实验的学生的信息，在线实验成绩等的页面
     * @return
     */
    @GetMapping("/getAllCheckHomework")
    public String checkExperiment(String id,ModelMap map)
    {
        //System.err.println(id);
        //将布置实验的id放入容器
        map.addAttribute("experimentTeaId",id);
        return prefix + "/checkHomework";
    }

    /**
     * 获取到已经做过实验的学生的信息，在线实验成绩等的页面
     * @return
     */
    @PostMapping("/getAllStudentMark")
    @ResponseBody
    public TableDataInfo getAllCheckExperiment(OnlineExperimentStu onlineExperimentStu)
    {
        startPage();
        List<OnlineExperimentStu> list=onlineExperimentStuService.getAllOnlineExperimentStu(onlineExperimentStu);
        //System.err.println(list);
        return getDataTable(list);
    }



    /**
     * 跳转到学生的在线实验页面
     * @param map
     * @return
     */
    @RequiresPermissions("system:studentExperiment:view")
    @GetMapping("/getStudentExperiment")
    public String getStudentExperiment(ModelMap map){
        //获取到当前登录用户id(即学生用户id)
        long studentId=getSysUser().getUserId();
        //将用户id存入容器
        map.addAttribute("studentId",studentId);
        return prefix+"/myExperiment";
    }

    /**
     * 学生获取实验
     * @param status
     * @param pageNum
     * @return
     */
    @GetMapping("/toLookExperiment")
    @ResponseBody
    public PageInfo<LookExperiment> lookExperiment(int status, Integer pageNum){
         if(pageNum==null){
             pageNum=1;
         }
         //获取对象
         LookExperiment lookExperiment=new LookExperiment();
         lookExperiment.setStatus(status);
         lookExperiment.setStudentId(getSysUser().getUserId());
         //执行分页  错误没改
         PageHelper.startPage(pageNum,100000);
         //获取所有显示内容
         List<LookExperiment> lists=onlineExperimentStuService.getAllLookExperiment(lookExperiment);
         //获取分页信息
         PageInfo<LookExperiment> pageInfo=new PageInfo<LookExperiment>(lists);
         return pageInfo;
    }


    /**
     * 跳转到学生做实验页面
     * @param experimentTeaId
     * @param map
     * @return
     */
    @GetMapping("/makeExperiment")
    public String makeExperiment(String experimentTeaId,ModelMap map,HttpServletRequest request){
        //根据id查找内容
        LookExperiment lookExperiment=onlineExperimentStuService.getLookExperiment(experimentTeaId);
        //判断实验报告是否为空
        if(lookExperiment.getOnlineExperimentReport()==null){
            //如果为空，获取到实验管理教师提供的实验模板
            String experimentReport=onlineExperimentStuService.getExperimentReport(experimentTeaId);
            lookExperiment.setOnlineExperimentReport(experimentReport);
        }
        //将查找到的内容放入
        map.addAttribute("lookExperiment",lookExperiment);
        //获取用户
        Student student=new Student();
        //用户设置值
        student.setStudentId(getSysUser().getUserId());
        student.setExperimentTeaId(experimentTeaId);
        System.out.println(student);
        //获取token
        String token= JwtUtil.sign(student,40*1000*60);
        map.addAttribute("token",token);
        return prefix+"/lookExperiment";
    }

    /**
     * 修改重做
     * @param experimentTeaId
     * @param map
     * @return
     */
    @GetMapping("/makeExperiment1")
    public String makeExperiment1(String experimentTeaId,ModelMap map){
        //根据id查找内容
        LookExperiment lookExperiment=onlineExperimentStuService.getLookExperiment1(experimentTeaId,getSysUser().getUserId());
        //判断实验报告是否为空
        if(lookExperiment.getOnlineExperimentReport()==null){
            //如果为空，获取到实验管理教师提供的实验模板
            String experimentReport=onlineExperimentStuService.getExperimentReport(experimentTeaId);
            lookExperiment.setOnlineExperimentReport(experimentReport);
        }
        //将查找到的内容放入
        map.addAttribute("lookExperiment",lookExperiment);
        //获取用户
        Student student=new Student();
        //用户设置值
        student.setStudentId(getSysUser().getUserId());
        student.setExperimentTeaId(experimentTeaId);
        //获取token
        String token= JwtUtil.sign(student,40*1000*60);
        map.addAttribute("token",token);
        return prefix+"/lookExperiment";
    }


    /**
     * 学生提交实验成绩
     * @param onlineExperimentStu
     * @param request
     * @return
     */
    @PostMapping("/submitScore")
    @ResponseBody
    public AjaxResult submitScore(@RequestBody OnlineExperimentStu onlineExperimentStu, HttpServletRequest request){
        //System.out.println(onlineExperimentStu);
        //获取到当前登录用户
        User user=getSysUser();
        //获取到用户id
        onlineExperimentStu.setStudentId(user.getUserId());
        onlineExperimentStu.setSubmitDate(new Date());
        //执行插入语句(提交的是实验报告内容)    注：u3d实验信息在website/myexment/getExperimentResult已提交
        /**
         * 封装参数
         */
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("experimentTeaId",onlineExperimentStu.getExperimentTeaId());
        map.put("studentId",onlineExperimentStu.getStudentId());
        /**
         * 查找信息
         */
        OnlineExperimentStu onlineExperimentStu1=onlineExperimentStuService.getOnlineExperimentStu(map);
        /**
         * 判断是否有u3d实验信息
         */
        if(StringUtils.isEmpty(onlineExperimentStu1.getU3dScore()) || onlineExperimentStu1.getU3dScore()==null){
            return AjaxResult.error("请先进行虚拟实验操作，再提交实验报告");
        }
        try{
            boolean flag=onlineExperimentStuService.updateExperimentStu(onlineExperimentStu);
            return toAjax(true);
        }catch(Exception e){
            return AjaxResult.error("实验报告提交失败");
        }
    }

    /**
     * 查看实验成绩和教师评语
     */
    @GetMapping("/lookExperiment")
    public String getDetail(String experimentTeaId,ModelMap map){
        /**
         * 获取到表中的所有成绩数据
         */
        HashMap<String,Object> mmap=new HashMap<String,Object>();
        mmap.put("experimentTeaId",experimentTeaId);
        mmap.put("studentId",getSysUser().getUserId());
        OnlineExperimentStu onlineExperimentStu=onlineExperimentStuService.getOnlineExperimentStu(mmap);
        //根据布置的实验id获取到实验时间等信息
        OnlineExperimentTea onlineExperimentTea=onlineExperimentStuService.getOnlineExperimentTea(experimentTeaId);
        onlineExperimentStu.setOnlineExperimentTea(onlineExperimentTea);
        /**
         * 将数据存放入容器
         */
        map.addAttribute("onlineExperimentStu",onlineExperimentStu);
        return prefix+"/experimentScore";
    }

    /**
     * 批改学生提交的实验成绩页面
     */
    @GetMapping("/pagaiStudentExperiment/{id}")
    public String pagaiStudentExperiment(@PathVariable("id") String message,ModelMap map){
        String[] array=message.split(",");
        //根据教师布置的实验id和学生的id去查找内容
        HashMap<String,Object> mmap=new HashMap<String, Object>();
        mmap.put("experimentTeaId",array[0]);
        mmap.put("studentId",array[1]);
        OnlineExperimentStu onlineExperimentStu=onlineExperimentStuService.getOnlineExperimentStu(mmap);
        //根据布置的实验id获取到实验时间等信息
        OnlineExperimentTea onlineExperimentTea=onlineExperimentStuService.getOnlineExperimentTea(array[0]);
        onlineExperimentStu.setOnlineExperimentTea(onlineExperimentTea);
        //將信息存入容器
        map.put("onlineExperimentStu",onlineExperimentStu);
        return prefix+"/experimentScore2";
    }


    /**
     * 评分
     * @param onlineExperimentStu
     * @return
     */
    @PostMapping("/checkHomeWork")
    @ResponseBody
    public AjaxResult checkHomeWork(@RequestBody OnlineExperimentStu onlineExperimentStu){
          //System.out.println(onlineExperimentStu);
          boolean flag=onlineExperimentStuService.updateExperimentStu(onlineExperimentStu);
          return toAjax(flag);
    }


    /**
     * 老师查看学生分数
     * @param experimentTeaId
     * @param studentId
     * @param map
     * @return
     */
    @GetMapping("/lookStudentScore")
    public String getDetail(String experimentTeaId,long studentId,ModelMap map){
        /**
         * 获取到表中的所有成绩数据
         */
        HashMap<String,Object> mmap=new HashMap<String, Object>();
        mmap.put("experimentTeaId",experimentTeaId);
        mmap.put("studentId",studentId);
        OnlineExperimentStu onlineExperimentStu=onlineExperimentStuService.getOnlineExperimentStu(mmap);
        //根据布置的实验id获取到实验时间等信息
        OnlineExperimentTea onlineExperimentTea=onlineExperimentStuService.getOnlineExperimentTea(experimentTeaId);
        onlineExperimentStu.setOnlineExperimentTea(onlineExperimentTea);
        /**
         * 将数据存放入容器
         */
        map.addAttribute("onlineExperimentStu",onlineExperimentStu);
        return prefix+"/experimentScore";
    }


    /**
     * 删除【请填写功能名称】
     */
    @RequiresPermissions("system:onlineExperimentStu:remove")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(onlineExperimentStuService.deleteExperimentTea(ids));
    }


    /**
     * 修改已经布置的实验的时间
     */
    @GetMapping("/editTime/{id}")
    public String edit(@PathVariable("id") String onlineExperimentId, ModelMap mmap)
    {
        //根据id获取布置的实验
        OnlineExperimentTea onlineExperimentTea=onlineExperimentStuService.getExperimentTeaById(onlineExperimentId);
        //将获取到的布置的实验放入容器
        mmap.addAttribute("onlineExperimentTea",onlineExperimentTea);
        return prefix + "/editExperimentTime";
    }

    /**
     * 修改已经布置的实验的时间
     */
    @PostMapping("/editSaveTime")
    @ResponseBody
    public AjaxResult editSaveTime(OnlineExperimentTea onlineExperimentTea)
    {
        return toAjax(onlineExperimentStuService.editExperimentTeaTime(onlineExperimentTea));
    }


    /**
     * 导出学生的成绩
     */
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(String experimentTeaId)
    {
        List<OnlineExperimentStu> list = onlineExperimentStuService.getResult(experimentTeaId);
        ExcelUtil<OnlineExperimentStu> util = new ExcelUtil<OnlineExperimentStu>(OnlineExperimentStu.class);
        return util.exportExcel(list, "实验成绩");
    }

}
